package bdz.projects.atwww.electrolux.ui
{
	import bdz.common.utils.MathUtils;
	import bdz.projects.atwww.electrolux.events.ELUIEvent;
	import bdz.projects.atwww.electrolux.model.core.ELPrograms;
	import bdz.projects.atwww.electrolux.ui.core.ELCoreUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import gs.TweenMax;
	import gs.easing.Strong;

	public class ELRotaryControl extends ELCoreUI {
		
		protected var _base:MovieClip;
		protected var _radius:Number;
		protected var _center:Point;
		protected var _degreeIncrement:Number = 30;
		protected var _marker:MovieClip;
		protected var _rotationValue:Number = 300;
		protected var _prevRotation:Number = 0;
		protected var _timer:Timer;
		protected var _mainHolder:MovieClip;
		protected var _programs:ELPrograms;
		protected var _sequence:Array = [9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8];
		protected var _reference:Dictionary;
		protected var _rotateSpeed:Number = .4;
		protected var _createReference:Boolean = true;
		protected var _customBase:MovieClip;
		public function ELRotaryControl(rad:Number, marker:MovieClip) {
			_radius = rad;
			_marker = marker;
			super(getAbstract());
		}
		override protected function initializeComponent():void {
			
			
			_mainHolder = new MovieClip();
			_mainHolder.mouseEnabled = false;
			
			if(_customBase){
				_base = _customBase;
			}else{
				_base = new MovieClip();
				_base.graphics.beginFill(0xeeeeee, 0);
				_base.graphics.drawCircle(_radius, _radius, _radius);
				_base.graphics.endFill();
				_base.cacheAsBitmap = true;
				addChild(_base);
			}
			
			_base.addEventListener(MouseEvent.MOUSE_DOWN, onBaseMouseDown);
			
			_center = new Point(_radius, _radius);
			
			_mainHolder.x = _center.x;
			_mainHolder.y = _center.y;
			
			_marker.mouseEnabled = _marker.mouseChildren = false;
			_marker.rotation = _rotationValue;
			_mainHolder.addChild(_marker);
			
			addChild(_mainHolder);
			
			
		}
		
		protected function initializePrograms():void{
			updateReferences();
			onRotationComplete();
		}
		protected function snapValue(n:Number):Number{
			
			return Math.floor(Math.round(n/_degreeIncrement))*_degreeIncrement;
		}
		protected function handleRotations():void{
			
			var rot:Number = MathUtils.getAngle({x:mouseX, y:mouseY}, _center, false);
			if(rot < 0){
				rot = Math.floor(360+rot);
			}
			_rotationValue = snapValue(rot);
			if(_rotationValue == 360){
				_rotationValue = 0;
			}
			if(_prevRotation != _rotationValue && Math.abs(_rotationValue-_prevRotation) > 20){
				TweenMax.killTweensOf(_marker);
				var increment:Number = Math.ceil(_rotationValue-Math.floor(_marker.rotation));
				var finc:Number = increment%360;
				if(finc > 180){
					finc -= 360;
				}
				TweenMax.to(_marker, _rotateSpeed, {rotation:String(finc), ease:Strong.easeOut, onComplete:onRotationComplete});
				_prevRotation = _rotationValue;
			}
			
		}
		protected function applyMouseMoveListener(b:Boolean):void{
			if(b){
				addEventListener(MouseEvent.MOUSE_MOVE, onBaseMouseMove);
				stage.addEventListener(MouseEvent.MOUSE_MOVE, onBaseMouseMove);
				_base.addEventListener(MouseEvent.MOUSE_UP, onBaseMouseUp);
				stage.addEventListener(MouseEvent.MOUSE_UP, onBaseMouseUp);
			}else{
				removeEventListener(MouseEvent.MOUSE_MOVE, onBaseMouseMove);
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onBaseMouseMove);
				_base.removeEventListener(MouseEvent.MOUSE_UP, onBaseMouseUp);
				stage.removeEventListener(MouseEvent.MOUSE_UP, onBaseMouseUp);
			}
		}
		protected function onRotationComplete():void{
			dispatchEvent(new ELUIEvent(ELUIEvent.ACTIVATE_PROGRAM, _reference[_rotationValue/_degreeIncrement]));
		}
		protected function onBaseMouseMove(evt:MouseEvent):void{
			handleRotations();
		}
		protected function onBaseMouseUp(evt:MouseEvent):void{
			applyMouseMoveListener(false);
			handleRotations();
		}
		protected function onBaseMouseDown(evt:MouseEvent):void{
			applyMouseMoveListener(true);
		}
		public function updateReferences():void{
			_reference = new Dictionary();
			var counter:Number = 0;
			for each(var n:Number in _sequence){
				_reference[n] = _programs[counter];
				counter++;
			}
		}
		public function set programs(p:ELPrograms):void{
			_programs = p;
			initializePrograms();
		}
		public function get programs():ELPrograms{
			return _programs;
		}
		
	}
}